﻿
using Dapper;
using HBase;
using Net.http;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace hCommand
{
    public abstract class WcsFinshParamLocEngine<CS> : AbsLocComp where CS : AbsCoreStock  {
        public long paramId;
        public int realStn;
        protected CoreTaskParam ctp;
        protected List<CoreTaskParam> otherCtpList;
        protected CoreTask ct;
        protected CS cs;

        protected virtual void doOther() {
            foreach (CoreTaskParam o in otherCtpList) {
                if (o.src == ctp.src && o.des == TaskNoUtil.virtual_Loc) {
                    o.status = (int)CoreTaskParamStatus.执行完成;
                    string sql = @"update Core_Task_Param set  status=@status where id=@id";
                    exec(sql, o);
                }
            }
        }
      
        protected override TransRes Execute() {
            initData( );
            int oldDes = ctp.des;
            if( realStn != 0 ) {
                ctp.des = realStn;
            }
            doOther( );
            finshParam( );
            LocCoreUtil.UpdataWhLoc(this ,( int ) ctp.src ,0);//清除原货位 
            if (oldDes!=ctp.des) {
                LocCoreUtil.UpdataWhLoc(this,oldDes,0);//清除原货位 
            }
            if (needFinsh( )) {
                LocOrderUtil.finshCoreTask(this, ct.id);  
                if (I18nConfig.isLoc(ctp.des)) {//入库或者移库 
                    if (BUtil.eq(cs.status, StockStatus.盘库回库)) {
                        updateStockStatus(  StockStatus.盘库待确认, ct.stockId);
                        LocCoreUtil.UpdataWhLoc(this, ctp.des, cs.deepDiff);
                    } else if (BUtil.eq(cs.status, StockStatus.入库中,StockStatus.移库中)) {
                        updateStockStatus(  StockStatus.库存, ct.stockId);
                        LocCoreUtil.UpdataWhLoc(this, ctp.des, cs.deepDiff);
                        inFinsh( );
                    } else {
                        throw new Exception(ctp.taskNo+"入库任务状态异常");
                    }
               
               
                } else if (!I18nConfig.isLoc(ctp.des)) {//出库
                    if (BUtil.eq(cs.status, StockStatus.盘库出库)) {
                        updateStockStatus( StockStatus.盘库库外, ct.stockId);
                    } else if (BUtil.eq(cs.status, StockStatus.出库中)) {
                        updateStockStatus(  getOutTaskedStatus( ), ct.stockId);
                    } else {
                        throw new Exception(ctp.taskNo + "出库任务状态异常");
                    }
                   
                    LocCoreUtil.UpdataWhLoc(this, ctp.des, cs.deepDiff);
                    outFinsh( );
                }
            } else {
                LocCoreUtil.UpdataWhLoc(this, ctp.des, cs.deepDiff);
            }   

            afterParamFinsh( );
            return new TransRes();
        }
        protected virtual TransRes afterParamFinsh( ) {
            return new TransRes();
        }
       void  updateStockStatus( StockStatus status, long id) {
            exec(  "update Core_stock set Status=@status   where id=@id ", new {
                id  ,
                status  });
        }
        public virtual void inFinsh( ) {

        }
        public virtual void outFinsh( ) {

        }


        void finshParam() {
            string sql = @"update Core_Task_Param set  status=" + ( int ) CoreTaskParamStatus.执行完成 + " where id=@id";
            exec (sql ,new { id = paramId });


            if ( ctp.type != ( byte ) TaskParamType.机器人分解 ) {
                string updateSorestockSql = @"update Core_stock set  loc_Id=@stn, change_Time=sgj_get_sys_now where id=@id";
                exec(updateSorestockSql ,new { id = ct.stockId ,stn = ctp.des  });

            } else {
                string updateSorestockSql = @"update Core_stock set   change_Time=sgj_get_sys_now where id=@id";
              exec(updateSorestockSql ,new { id = ct.stockId  });


            }

        }

        
        void initData() {
            string paramSql = "SELECT p1.*  FROM  [Core_Task_Param] p1  join [Core_Task_Param] p2 on p1.task_Id = p2.task_Id  where p2.id = " + paramId;
            otherCtpList = query<CoreTaskParam>(paramSql  );
            foreach( CoreTaskParam p in otherCtpList ) {
                if( p.id == paramId ) {
                    ctp = p;
                    otherCtpList.Remove(p);
                    break;
                }
            }
            if( ctp == null ) {
                throw new Exception("任务参数不存在" + paramId);
            }
            ct = getById<CoreTask>(ctp.taskId);
            cs = getById<CS>(ct.stockId);
            
        }

        protected virtual TransRes afterFinsh( ) {
            return new TransRes();
        }

        protected virtual bool needFinsh( ) {
        
            foreach ( CoreTaskParam ctp in otherCtpList ) {
                if( ctp.status < ( int ) CoreTaskParamStatus.执行完成 ) {
                    return false;
                }
            }

            return ct.des == ctp.des;
        }

        protected virtual  bool isPk( ) {
            return  BUtil.eq(cs.status, StockStatus.待盘库  , StockStatus.盘库出库 , StockStatus.盘库库外  , StockStatus.盘库回库  , StockStatus.盘库待确认)
 
               ;
        }


        protected virtual StockStatus getOutTaskedStatus( ) {
            if (realStn > 100) {
                return StockStatus.库外;
            } else {
                return StockStatus.直接删除;
            }
          
        }

   

    }
}
